import { createRouter, createWebHashHistory } from 'vue-router'
import DoctorDashboard from '@/views/doctor/DashboardView.vue'
import ManagerDashboard from '@/views/manager/DashboardView.vue'
import NurseDashboard from '@/views/nurse/DashboardView.vue'

const routes = [
  //根路径，根据用户角色重定向到对应首页
  {
    path: '/',
    name: 'redirect',
    redirect: (to) => {
      // 根据用户角色重定向到对应首页
      const user = JSON.parse(localStorage.getItem('user'))
      if (!user) return '/login'

      switch (user.role_name) {
        case 'doctor':
          return '/doctor/home'
        case 'manager':
          return '/manager/home'
        case 'nurse':
          return '/nurse/home'
        default:
          return '/login'
      }
    }
  },
  //医生页面
  {
    path: '/doctor',
    name: 'doctor',
    meta: {
      title: "智医云联-医生工作台",
      requiresAuth: true,
      requiredRole: 'doctor'
    },
    component: DoctorDashboard,
    redirect: '/doctor/home',
    children: [
      //医生工作台
      {
        path: 'home',
        name: 'DoctorDashboard',
        meta: {
          title: "智医云联-医生工作台"
        },
        components: {
          default: () => import('@/views/doctor/DoctorMainView.vue'),
        }
      },
      //预约管理
      {
        path: 'reservation',
        name: 'DoctorReservationManager',
        meta: {
          title: "智医云联-预约挂号管理"
        },
        component: () => import('@/views/doctor/ReservationManagerView.vue')
      },
      // router/doctor.js
      {
        path: '/doctor/patient-list',
        name: 'PatientList',
        component: () => import('@/views/doctor/PatientListView.vue'),
        meta: { title: '智医云联-患者档案管理' }
      },
      {
        path: '/doctor/medicalrecord-templates',
        name: 'MedicalRecordTemplates',
        component: () => import('@/views/doctor/MedicalRecordTemplates.vue'),
        meta: { title: '智医云联-病历模板' }
      },
      //病历管理
      {
        path: 'medicalrecord',
        name: 'DoctorMedicalRecordManager',
        meta: {
          title: "智医云联-病历管理"
        },
        component: () => import('@/views/doctor/MedicalRecordManagerView.vue')
      },
      //诊断记录
      {
        path: 'diagnosis-list',
        name: 'DoctorDiagnosisList',
        meta: {
          title: "智医云联-诊断记录"
        },
        component: () => import('@/views/doctor/DiagnosisListView.vue')
      },
      //我的排班
      {
        path: 'doctor-schedule',
        name: 'DoctorSchedule',
        meta: {
          title: "智医云联-我的排班"
        },
        component: () => import('@/views/doctor/DoctorScheduleView.vue')
      },
    ]
  },
  //管理员页面
  {
    path: '/manager',
    name: 'ManagerDashboard',
    meta: {
      title: "智医云联-管理员首页",
      requiresAuth: true,
      requiredRole: 'manager'
    },
    component: ManagerDashboard,
    redirect: '/manager/home',
    children: [
      //管理员工作台
      {
        path: 'home',
        name: 'ManagerHome',
        meta: {
          title: "智医云联-管理员工作台"
        },
        components: {
          default: () => import('@/views/manager/ManagerMainView.vue'),
        }
      },
      {
        path: 'department',
        name: 'ManagerDepartmentManager',
        meta: {
          title: "智医云联-科室管理"
        },
        component: () => import('@/views/manager/DepartmentManagerView.vue')
      },
      {
        path: 'doctor',
        name: 'ManagerDoctorManager',
        meta: {
          title: "智医云联-医生管理"
        },
        component: () => import('@/views/manager/DoctorManagerView.vue')
      },
      {
        path: 'nurse',
        name: 'NurseManager',
        meta: {
          title: "智医云联-护士管理"
        },
        component: () => import('@/views/manager/NurseManagerView.vue')
      },
      //药剂师管理
      {
        path: 'pharmacist',
        name: 'PharmacistManager',
        meta: {
          title: "智医云联-药剂师管理"
        },
        component: () => import('@/views/manager/PharmacistManagerView.vue')
      },
      //财务人员管理
      {
        path: 'finance',
        name: 'FinanceStaffManager',
        meta: {
          title: "智医云联-财务人员管理"
        },
        component: () => import('@/views/manager/FinanceStaffManagerView.vue')
      },
      //行政人员管理
      {
        path: 'adminstaff',
        name: 'AdminStaffManager',
        meta: {
          title: "智医云联-行政人员管理"
        },
        component: () => import('@/views/manager/AdminStaffManagerView.vue')
      },
      {
        path: 'patient',
        name: 'ManagerPatientManager',
        meta: {
          title: "智医云联-患者管理"
        },
        component: () => import('@/views/manager/PatientManagerView.vue')
      },
      // 费用配置
      {
        path: 'fee-configuration',
        name: 'ManagerFeeConfiguration',
        meta: {
          title: "智医云联-费用配置"
        },
        component: () => import('@/views/manager/FeeConfigurationView.vue')
      },
      {
        path: 'schedule-templates',
        name: 'ManagerScheduleManager',
        meta: {
          title: "智医云联-排班模板管理"
        },
        component: () => import('@/views/manager/ScheduleTemplateView.vue')
      },
      // 排班实例管理
      {
        path: 'schedule-instances',
        name: 'ManagerScheduleInstances',
        meta: {
          title: "智医云联-排班实例管理"
        },
        component: () => import('@/views/manager/ScheduleInstanceView.vue')
      },
      {
        path: 'billing',
        name: 'ManagerBillingManager',
        meta: {
          title: "智医云联-收费管理"
        },
        component: () => import('@/views/manager/BillingManagerView.vue')
      },
      {
        path: 'medicine',
        name: 'ManagerMedicineManager',
        meta: {
          title: "智医云联-药品管理"
        },
        component: () => import('@/views/manager/MedicineManagerView.vue')
      },
      {
        path: 'equipment',
        name: 'ManagerEquipmentManager',
        meta: {
          title: "智医云联-设备管理"
        },
        component: () => import('@/views/manager/EquipmentManagerView.vue')
      },
      {
        path: 'statistics',
        name: 'ManagerStatisticsManager',
        meta: {
          title: "智医云联-数据统计"
        },
        component: () => import('@/views/manager/StatisticsManagerView.vue')
      },
      {
        path: 'settings',
        name: 'ManagerSettings',
        meta: {
          title: "智医云联-系统设置"
        },
        component: () => import('@/views/manager/SettingsManagerView.vue')
      },
    ]
  },
  //护士页面
  {
    path: '/nurse/',
    name: 'NurseHome',
    meta: {
      title: "智医云联-护士首页",
      requiresAuth: true,
      requiredRole: 'nurse'
    },
    redirect: '/nurse/home',
    component: NurseDashboard,
    children: [
      //护士工作台
      {
        path: 'home',
        name: 'NurseDashboard',
        meta: {
          title: "智医云联-护士工作台"
        },
        components: {
          default: () => import('@/views/nurse/NurseHomeView.vue'),
        }
      },
      //预约管理
      //患者管理
      {
        path: 'patientmanager',
        name: 'NursePatientManager',
        meta: {
          title: "智医云联-患者管理"
        },
        component: () => import('@/views/nurse/PatientManagerView.vue')
      },
      //病历管理
      {
        path: 'medicalrecord',
        name: 'NurseMedicalRecord',
        meta: {
          title: "智医云联-病历管理"
        },
        component: () => import('@/views/nurse/MedicalRecords.vue')
      },
    ]
  },
  //公共设备页面
  {
    path: '/self-service',
    name: 'SelfService',
    meta: {
      title: "智医云联-自助服务机",
      requiresAuth: false
    },
    redirect: "/self-service/home",
    component: () => import('@/views/public/PublicDashboardView.vue'),
    children: [
      //自助机首页
      {
        path: "home",
        name: "SelfServiceMachine",
        meta: {
          title: "智医云联-自助机",
          requiresAuth: false
        },
        component: () => import('@/views/public/SelfServiceMachine.vue')
      },
      // 扫描或注入医保号登记
      {
        path: "registration-scan",
        name: "registration-scan",
        meta: {
          title: "智医云联-医保认证",
          requiresAuth: false
        },
        component: () => import('@/views/public/RegistrationStartScanView.vue')
      },
      // 挂号选择父组件
      {
        path: "registration-selection",
        name: "registration-selection",
        meta: {
          title: "智医云联-自助挂号",
          requiresAuth: false
        },
        component: () => import('@/views/public/SelfServiceRegistration.vue')
      },
      // 门诊缴费扫码页面
      {
        path: "payment-start-scan",
        name: "PaymentStartScan",
        meta: {
          title: "智医云联-自助门诊缴费",
          requiresAuth: false
        },
        component: () => import('@/views/public/PaymentStartScanView.vue')
      },
      // 门诊缴费页面
      {
        path: "payment-fee-details",
        name: "FeeDetails",
        meta: {
          title: "智医云联-门诊缴费",
          requiresAuth: false
        },
        component: () => import('@/views/public/PaymentFlowView.vue')
      },

    ]
  },
  //公共设备页面
  {
    path: "/publicdevice",
    name: "PublicDevice",
    meta: {
      title: "公共设备",
      requiresAuth: false
    },
    redirect: "/publicdevice/view",
    component: () => import('@/views/public/PublicDashboardView.vue'),
    children: [
      {
        path: "view",
        name: "PublicDeviceView",
        meta: {
          title: "公共设备展示",
          requiresAuth: false
        },
        component: () => import('@/views/public/publicdevice/PublicDeviceView.vue')
      },
      {
        path: "queue",
        name: "QueueSystem",
        meta: {
          title: "排队叫号系统",
          requiresAuth: false
        },
        component: () => import('@/views/public/publicdevice/QueueSystem.vue')
      },
    ]
  },
  {
    path: '/tts',
    name: 'tts',
    meta: {
      title: "语音合成",
      requiresAuth: false
    },
    component: () => import('@/views/TTSView.vue')
  },
  {
    path: '/ttsstream',
    name: 'ttsstream',
    meta: {
      title: "流式语音合成",
      requiresAuth: false
    },
    component: () => import('@/views/TTSStreamView.vue')
  },
  //登录页面
  {
    path: '/login',
    name: 'login',
    meta: {
      title: "智医云联-登录",
      requiresAuth: false
    },
    component: () => import('@/views/LoginView.vue')
  },
  //生成二维码和条形码测试页面
  {
    path: "/demo",
    name: "demo_qrcode",
    meta: {
      title: "医保二维码生成",
      requiresAuth: false
    },
    component: () => import('@/views/demo/TestQrCodeView.vue')
  },
  //其他未匹配到的路径重定向到登录页
  {
    path: '/:pathMatch(.*)*',
    name: 'not-found',
    redirect: '/login'
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token")
  const user = JSON.parse(localStorage.getItem('user') || 'null')

  // 不需要认证的页面直接放行
  if (!to.meta.requiresAuth) {
    next()
    return
  }

  // 检查是否登录
  if (!token || !user) {
    next('/login')
    return
  }

  // 检查角色权限
  const requiredRole = to.meta.requiredRole
  if (requiredRole && user.role_name !== requiredRole) {
    // 角色不匹配，重定向到对应角色的首页
    switch (user.role) {
      case 'doctor':
        next('/doctor/home')
        break
      case 'manager':
        next('/manager/home')
        break
      case 'nurse':
        next('/nurse/home')
        break
      default:
        next('/login')
    }
    return
  }

  next()
})

export default router
